#include "Question.h"
#include "ResourceManager.h"
#include <d3dx9.h>

Question::Question(int id,float x, float y, int type, int state)
{
	_sprite = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.BrickAll),300, 0);
	_id = id;
	_x = x;
	_y = y;
	_type = type;
	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();
	_state = state;

	_sprite->SetFrame(2,4);
	
	isMoveUp = false;
	_xPre = _x;
	_yPre = _y;
	_vx = 0;
	_vy = 0;
	_accelY = 0;
	//_state = BS_NORMAL; //mac dinh object state la NORMAL:1
	if (_state == BS_NONE)
	{
		_sprite->SetFrame(1,1);
		isMoveUp = true;
	} 

}

Question::~Question(void)
{
	if (_sprite)
	{
		delete _sprite;
	}
	
}

void Question::Update(list<Object *> *_list, float time)
{
	list<Object*>::iterator i;
	Object* obj;

	switch(_state)
	{
	case BS_NORMAL:

		_sprite->Update(time);
		break;

	case BS_MOVEUP:
		if (isMoveUp == false)
		{
			_vy -= _accelY * time;
			_y += _vy * time + 1/2 * _accelY * time * time;
			
			//Xu li dung gach voi coin,...
			for (i = _list->begin(); i != _list->end(); i++)
			{
				obj = *i;

				//Box box = this->getBox(time);
				Box box(this->getPosX() + _width/2,
					this->getPosY() - this->getHeight(), 
					this->getWidth()/2, 
					this->getHeight(), 
					0, 
					_vy * time + 1.0f/2.0f * _accelY * time * time);

				//Box block = obj->getBox(time);
				Box block(obj->getPosX() + obj->getWidth()/2,
					obj->getPosY() - obj->getHeight(),// - 0.11,
					obj->getWidth()/2,
					obj->getHeight()
					);

				Box boxphase = Collision::_GetSweptBroadphaseBox(box);
				if (Collision::_AABBCheck(boxphase, block))
				{
					switch (obj->getIDObjType())
					{
					case 14:	//coin
					case 46:	//coin an
					case 39:	//5 coin hidden
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_ACTIVING);
							//obj->setIsChangeState(true);
						}
						//choi an gian nhe
						break;
					case 16:	//nam mang
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_ACTIVING);
						}
						break;
					case 15:
						if (obj->getObjState() == IS_IDLE)
						{
							//obj->_isActiveMushRoom = true;
							obj->changeState(IS_FLOWER);
						}
					case 17:	//nam to, hoa
						if (obj->getObjState() == IS_IDLE)// && obj->_isActiveMushRoom == false)
						{
							//obj->_isActiveMushRoom = true;
							obj->changeState(IS_ACTIVING);
						}
						
						break;

					case 18:	//sao
						if (obj->getObjState() == IS_IDLE)
						{
							obj->changeState(IS_ACTIVING);
						}
						break;

					case 9:		//goomba
					case 12:		//koopba
						obj->changeState(ES_FALL);
						break;


					}
				}
			}

			if (_y <= _yPre)
			{
				changeState(BS_NONE);
			}
			_sprite->Update(time);
		}
		break;

	case BS_NONE:
		_sprite->Update(time);
		break;
	}
}


void Question:: Render(int vpX, int vpY)
{
	switch(_state)
	{
	case BS_NORMAL:
	case BS_NONE:
	case BS_MOVEUP:
		_sprite->Render(_x, _y, vpX, vpY);
		break;

	}
}

void Question::changeState(int state)
{
	_state = state;
	switch (_state)
	{
	case BS_NORMAL:
		isMoveUp = true;
		_y = _yPre;
		_vy = 0;
		//_accelY = 0;
		_accelY = 0;
		break;
	case BS_MOVEUP:
		_vy = 1.0;
		_accelY = QUESTION_GRAVITY;
		_sprite->SetFrame(1,1); //frame emptybox
		break;

	case BS_NONE:
		isMoveUp = true;
		_y = _yPre;
		_vy = 0;
		//_accelY = 0;
		_accelY = 0;
		break;

	case BS_REMOVE:
		break;
	}
}

OBJECT_TYPE Question::getObjectType()
{
	return OBJECT_LAND;
}